
***********************************************
** Household Regressions - Prices On Incomes **
***********************************************
use "$data\61_round_Sch1.dta", replace
su pce*
cou
// replace pce = pce/100
// replace pce_365 = pce_365/100


rename HHID hhid 

	gen durable_temp = totalConValue365 if itemCode==659
	egen durable = min(durable_temp),by(hhid)
	replace durable = 0 if durable==.
	replace pce_365 = pce_365 - 30/365*durable/hhsize


/* Dropping all itemCodes which end with 9 as they represent sub-totals except 279 which is salt */
gen last_digit = itemCode - floor(itemCode/10)*10
drop if last_digit == 9&itemCode~=279

* Bring in if itemCode should have price reported. Drop if not.
cap drop _merge
merge m:1 itemCode using "$concordances\nss_items_priceReported"
* Only Salt is unmerged as the using didn't have itemCodes which end in 9 which was always subtotals except Salt.
tab itemCode if _merge==1
drop if itemCode~=279&_merge==1
tab itemCode if price_yes==0
drop if  price_yes==0

/* Dropping Block 9 and 10 as it has no quantity data
   NOT dropping Block 11 which is durables although price data for some items is available in this. Was messing up the case where was excluding only the particular commodity from pce. 
   No longer binding due to non-price being removed earlier*/
drop if itemCode>=400&itemCode<=419
drop if itemCode>=420&itemCode<=549
*drop if itemCode>=550&itemCode<=659


cap drop _merge
compress
merge m:1 itemCode using "$concordances\nss_items_manu_mapping"
* Basically Salt is unmatched again
tab itemCode _merge if _merge~=3
drop if _merge==2
* Basically dropping PDS
tab itemCode if other_cleaning == 0
drop if other_cleaning==0


*Numbers look off for items in this code. Adjusted accordingly.
replace  totalConQty365 =  totalConQty365*1000 if itemCode>=390&itemCode<=399



destring St_Region, replace
gen state = floor(St_Region/10)
cou if state==.

* Price, using 30 days for food and 365 days for clothes and footwear
gen price = totalConValue365/totalConQty365 if itemCode>=360&itemCode<400
replace price = totalConValue30/totalConQty30 if itemCode<360
replace price = B11_q10/ B11_q8 if itemCode>=550

// gen price_noHome = price
// replace price_noHome = (totalConValue30 -  homeProduceValue30)/( totalConQty30- homeProduceQty30) if itemCode<360&homeProduceValue30~=.&homeProduceQty30~=.
// replace price_noHome = . if price_noHome==0  /* qtys were different but value was same. weird */

cou if price==.
tab itemCode if price==.
drop if price==.
gen ln_price = log(price)
// gen ln_price_noHome = log(price_noHome)
su price


* Remove the particular items value from total per-capita expenditure. Not removing for durables as these are removed from pce already
gen pce_365_exclude = pce_365 - totalConValue30/hhsize if itemCode<360
replace pce_365_exclude = pce_365 - 30/365*totalConValue365/hhsize if itemCode>=360&itemCode<400
replace pce_365_exclude = pce_365 if itemCode>=550
cou if pce_365_exclude==.
compare pce_365 pce_365_exclude
*replace  pce_365_exclude = pce_365 if  itemCode>=550
* If durables is 1 then don't need to exclude again
* replace  pce_365_exclude = pce_365 - 30/365*firstHandPurchaseValue365 if  itemCode>=550
gen ln_pce365 = log(pce_365)
gen ln_pce365_exclude = log(pce_365_exclude)
drop if ln_pce365==.
drop if ln_pce365_exclude==.

* Dropping item codes with less than 100 price observations. Most of these are items which shouldn't have price reported at all. There are some others like gramaphone, airconditioners etc
gen counter = 1
sort itemCode
by itemCode: gen item_counter = sum(counter)
egen item_no_obs = max(item_counter), by (itemCode)
tab itemCode if item_no_obs<100, su(item_no_obs)
drop counter item_counter item_no_obs

egen hh_tag = tag(hhid)		
drop if ln_pce365==.| ln_pce365_exclude==.|ln_price==.

/* Winsorize per capita income */
local per_drop 1
gsort -hh_tag pce_365
gen counter = _n /* The sort command before this line makes sure that if there are N unique hhid'd, then the first N observations are one observation each of these N households */
egen no_of_hh = max(counter) if hh_tag == 1, by(hh_tag)
local obs_winsor_low_bound = floor(no_of_hh*`per_drop'/100) + 1 
di `obs_winsor_low_bound'
local obs_winsor_upper_bound =  no_of_hh -  `obs_winsor_low_bound'
di `obs_winsor_upper_bound'
gen pce_winsor_extreme = pce_365[`obs_winsor_low_bound'] if counter<=`obs_winsor_low_bound'&hh_tag==1
replace pce_winsor_extreme = pce_365[`obs_winsor_upper_bound'] if counter>=`obs_winsor_upper_bound'&hh_tag==1
egen pce_365_winsor = min(pce_winsor_extreme),by(hhid)
replace pce_365_winsor = pce_365 if pce_365_winsor==.
su pce_365_winsor if hh_tag==1, de
cou if  pce_365_winsor> pce_365
cou if  pce_365_winsor< pce_365

/* Winsorize prices within itemCode, state, sector */
local per_drop 1
local min_size 1
drop counter 
gen counter = 1
sort itemCode State Sector ln_price
by itemCode State Sector: gen item_counter = sum(counter)
egen item_no_obs = max(item_counter), by (itemCode State Sector)
su item_no_obs, de
gen obs_winsor_low_bound = floor(item_no_obs*`per_drop'/100) + 1 
gen ln_price_lowbound = ln_price if obs_winsor_low_bound==item_counter
egen ln_price_low_replace = min(ln_price_lowbound) if item_counter<=obs_winsor_low_bound,by(itemCode State Sector)
gen obs_winsor_upper_bound =  item_no_obs -  obs_winsor_low_bound
gen ln_price_upperbound = ln_price if obs_winsor_upper_bound==item_counter
egen ln_price_upper_replace = min(ln_price_upperbound) if item_counter>=obs_winsor_upper_bound,by(itemCode State Sector)
gen ln_price_winsor = ln_price
replace ln_price_winsor = ln_price_upper_replace if ln_price_upper_replace~=.
replace ln_price_winsor = ln_price_low_replace if ln_price_low_replace~=.


gen ln_pce365_winsor = log(pce_365_winsor)
drop if ln_pce365==.| ln_pce365_winsor==.| ln_pce365_exclude==.
sum ln*

/* Marking all itemCodes which have the word "other" mentioned in it */
gen other_dummy = 0
replace other_dummy = 1 if inlist(itemCode,106,114,122,150,153,167,174,186,212,224,247,255,257,286,298,308,315,327,335,353,373,387,392,394,404,406,414,424,445,438,458)

egen fixed_effect = group(itemCode State Sector)

/* Everything that has been classified as manufacturing */
tab itemCode if ln_price~=.& ln_pce365~=.
return list
tab itemCode if ln_price~=.& ln_pce365~=.&other_dummy==0
return list
tab itemCode if ln_price~=.& ln_pce365~=.&other_dummy==0&itemCode<550
return list

**# Bookmark #4: education and spending by product
cap drop _merge
gen HHID=hhid
destring hhid, replace
merge m:1 HHID using "$data\HHID_edu_dep_adults.dta"
tab gen_edu, gen(EE)


gen e_illiterate=0 if !missing(gen_edu)
gen e_literate_below_primary=0 if !missing(gen_edu)
gen e_literate_primary=0 if !missing(gen_edu)
gen e_literate_middle=0 if !missing(gen_edu)
gen e_literate_secondary=0 if !missing(gen_edu)
gen e_above_secondary=0 if !missing(gen_edu)

label variable e_illiterate "Illiterate"
label variable e_literate_below_primary "Below Primary"
label variable e_literate_primary "Primary"
label variable e_literate_middle "Middle"
label variable e_literate_secondary "Secondary"
label variable e_above_secondary "Above Secondary"

replace e_illiterate=1 if gen_edu=="01"
replace e_literate_below_primary=1 if gen_edu=="02"|gen_edu=="03"|gen_edu=="04"
replace e_literate_primary=1 if gen_edu=="05"
replace e_literate_middle=1 if gen_edu=="06"
replace e_literate_secondary=1 if gen_edu=="07"
replace e_above_secondary=1 if gen_edu=="08"|gen_edu=="10"|gen_edu=="11"
reghdfe ln_price e_literate_below_primary e_literate_primary e_literate_middle e_literate_secondary e_above_secondary if other_dummy==0&itemCode<550, absorb(fixed_effect) vce(cluster hhid)
coefplot, drop(_cons) labels xlabel(0 (0.02) 0.14)
**********
******FIGURE #3**********
**********
graph export $results\F3_Education_price_20250305.png, replace

gen block=.
replace block=5 if  itemCode>=101&itemCode<=339 // food, pan, tobacco
replace block=6 if  itemCode>339&itemCode<=359 // fuel and light
replace block=7 if  itemCode>359&itemCode<=389 // clothing
replace block=8 if  itemCode>389&itemCode<=399 //footwear
replace block=9 if  itemCode>399&itemCode<=419 // education and medical (instutitional)
replace block=10 if  itemCode>419&itemCode<=549  //misc goods and services
replace block=11 if  itemCode>549&itemCode<=700  // purchase and construction of durable goods      

**# Bookmark #1
egen fixed_effect_i_s_r = group(itemCode State Sector)
egen fixed_effect_s_r = group(State Sector)
// Make product specific elasticity with this code
tab itemCode, gen(II)
drop if itemCode>=550

gen itemCodeCoef=.
forv i=1(1)205{
	local iter `=`iter'+1'
	capture noisily reghdfe ln_price ln_pce365 if other_dummy==0&itemCode<550& II`i'==1, absorb(fixed_effect_s_r) vce(cluster hhid) 
	capture noisily replace itemCodeCoef=_b[ln_pce365] if _n==`iter'
}

sum itemCodeCoef, det

** Histogram of the bank-specific coefficients
label variable itemCodeCoef "Item-code-specific elasticity"
hist itemCodeCoef, freq xtitle("Product-specific price elasticity to per capita expenditure") xlabel(,nogrid) ylabel(,nogrid)
drop II*
**************************************************
*************************Figure 4**********************
**************************************************
**# Bookmark #1
graph export $results\F4_20250306_Histogram_consumer_elasticity.png, replace

label variable ln_price_winsor "log(price)"
label variable ln_pce365 "log(per-capita expenditure)"
label variable ln_pce365_winsor "log(per-capita expenditure): winsored"
label variable ln_pce365_exclude "log(per-capita expenditure): exclude own-product"


**************************************************
*************************TABLE 1**********************
**************************************************
eststo clear
	eststo: qui reghdfe ln_price_winsor ln_pce365 if other_dummy==0&itemCode<550, absorb(itemCode ) vce(cluster hhid)
	estadd scalar Observations=e(N_full)
	estadd scalar Single=e(num_singletons) 
	estadd local Fixed0 "N/A"
	estadd local Fixed1 "Yes"
	estadd local Fixed2 "No"
	estadd local Fixed3 "No"
	estadd local Products "188"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Household"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
	eststo: qui reghdfe ln_price_winsor ln_pce365 if other_dummy==0&itemCode<550, absorb(fixed_effect_s_r itemCode) vce(cluster hhid)
	estadd scalar Observations=e(N_full)
	estadd scalar Single=e(num_singletons) 
	estadd local Fixed0 "N/A"
	estadd local Fixed1 "Yes"
	estadd local Fixed2 "Yes"
	estadd local Fixed3 "No"
	estadd local Products "188"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Household"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
	eststo: qui reghdfe ln_price_winsor ln_pce365 if other_dummy==0&itemCode<550, absorb(fixed_effect_i_s_r) vce(cluster hhid)
	estadd scalar Observations=e(N_full)
	estadd scalar Single=e(num_singletons) 
	estadd local Fixed0 "N/A"
	estadd local Fixed1 "N/A"
	estadd local Fixed2 "N/A"
	estadd local Fixed3 "Yes"
	estadd local Products "188"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Household"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
	eststo: qui reghdfe ln_price_winsor ln_pce365_winsor if other_dummy==0&itemCode<550, absorb(fixed_effect) vce(cluster hhid)
	estadd scalar Observations=e(N_full)
	estadd scalar Single=e(num_singletons) 
	estadd local Fixed0 "N/A"
	estadd local Fixed1 "N/A"
	estadd local Fixed2 "N/A"
	estadd local Fixed3 "Yes"
	estadd local Products "188"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Household"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
	eststo: qui reghdfe ln_price_winsor ln_pce365_exclude if other_dummy==0&itemCode<550, absorb(fixed_effect_i_s_r) vce(cluster hhid)
	estadd scalar Observations=e(N_full)
	estadd scalar Single=e(num_singletons) 
	estadd local Fixed0 "N/A"
	estadd local Fixed1 "N/A"
	estadd local Fixed2 "N/A"
	estadd local Fixed3 "Yes"
	estadd local Products "188"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Household"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
// 	esttab, se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) scalars("FE Fixed Effects:") nocon label legend replace order(ln_pce365 ) 
	esttab, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("PR50_5 Price Ratio (75th to 25th percentile)" "PR500_5 Price Ratio (95th to 5th percentile)" "j \hline"  "Winsor Winsor" Observations "k \hline " "Fixed0 Block FE" "Fixed1 Product FE" "Fixed2 State x Rural FE"  "Fixed3 State x Rural x Product FE" "Products Number of Products" "Cluster SE clusters:"   "NumClust Number of Clusters" )
// 		esttab using $results\20250306_broader_consumer_tables.tex, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("PR50_5 Price Ratio (75th to 25th percentile)" "PR500_5 Price Ratio (95th to 5th percentile)" "j \hline"  "Winsor Winsor" Observations "k \hline " "Fixed0 Block FE" "Fixed1 Product FE" "Fixed2 State x Rural FE"  "Fixed3 State x Rural x Product FE" "Products Number of Products" "Cluster SE clusters:"   "NumClust Number of Clusters" )
**# Bookmark #2
		esttab using $results\T1_20250306_broader_consumer_tables_paper.tex, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("PR50_5 Price Ratio (75th to 25th percentile)" "PR500_5 Price Ratio (95th to 5th percentile)" "j \hline"  "Winsor Winsor" Observations "k \hline " "Fixed0 Block FE" "Fixed1 Product FE" "Fixed2 State x Rural FE"  "Fixed3 State x Rural x Product FE" "Products Number of Products" "Cluster SE clusters:"   "NumClust Number of Clusters" )


**# Alternative to education

gen pce_hh=pce_365*hhsize
gen ln_pce_hh=ln(pce_hh)
gen dep_ratio=dependants/hhsize
label variable ln_pce_hh "log (household expenditure)"
label variable dependants "Number of dependents"
label variable dep_ratio "Share of dependents in household"
eststo clear
eststo: reghdfe ln_price_winsor ln_pce_hh dependants  if other_dummy==0&itemCode<550, absorb(fixed_effect_i_s_r) vce(cluster hhid)
	estadd scalar Observations=e(N_full)
	estadd scalar Single=e(num_singletons)
	estadd local Fixed0 "Yes"
	estadd local Fixed1 "No"
	estadd local Fixed2 "No"
	estadd local Fixed3 "Yes"
	estadd local Products "188"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Household"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
	
eststo: reghdfe ln_price_winsor ln_pce_hh dep_ratio if other_dummy==0&itemCode<550, absorb(fixed_effect_i_s_r) vce(cluster hhid)
	estadd scalar Observations=e(N_full)
	estadd scalar Single=e(num_singletons)
	estadd local Fixed0 "Yes"
	estadd local Fixed1 "No"
	estadd local Fixed2 "No"
	estadd local Fixed3 "Yes"
	estadd local Products "188"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Household"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
	
		esttab using $results\T2_20250319_dependants2.tex, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars( "j \hline"  "Winsor Winsor" Observations "k \hline " "Fixed3 State x Rural x Product FE" "Products Number of Products" "Cluster SE clusters:"   "NumClust Number of Clusters" )
